package ugroupdao

import (
	"vpn-management-server/logger"
	"vpn-management-server/src/dao"
	"vpn-management-server/src/model/ugroupmodel"
)

func AddUGroup(UGroup *ugroupmodel.UGroup) error {
	sqlStr := "INSERT INTO ugroups(name,description,signer,a_id) value(?,?,?,?)"
	if _, err := dao.GMysqlDB.Conn.Exec(sqlStr, UGroup.Name, UGroup.Description, UGroup.Signer, -1); err != nil {
		logger.Logger.Error(err)
		return err
	} else {
		return nil
	}
}

func DeleteUGroupByID(id int) error {
	sqlStr := "DELETE FROM ugroups where id = ?"
	if _, err := dao.GMysqlDB.Conn.Exec(sqlStr, id); err != nil {
		logger.Logger.Error(err)
		return err
	} else {
		return nil
	}
}

func HasUGroupByName(name string) bool {
	var ugroup ugroupmodel.UGroup
	sqlStr := "SELECT * FROM ugroups where name = ?"
	err := dao.GMysqlDB.Conn.Get(&ugroup, sqlStr, name)
	if err == nil {
		return true
	}
	return false
}

func HasUGroupByID(ugid int) bool {
	var ugroup ugroupmodel.UGroup
	sqlStr := "SELECT * FROM ugroups where id = ?"
	err := dao.GMysqlDB.Conn.Get(&ugroup, sqlStr, ugid)
	if err == nil {
		return true
	}
	return false
}

func GetUGroupByName(name string) *ugroupmodel.UGroup {
	var ugroup ugroupmodel.UGroup
	sqlStr := "SELECT * FROM ugroups where name = ?"
	if err := dao.GMysqlDB.Conn.Get(&ugroup, sqlStr, name); err != nil {
		logger.Logger.Error(err)
		return nil
	}
	return &ugroup
}

func GetAdminIDbyUGroupID(ugid int) int {
	var aid int
	sqlStr := "SELECT a_id FROM ugroups where id = ?"
	if err := dao.GMysqlDB.Conn.Get(&aid, sqlStr, ugid); err != nil {
		logger.Logger.Error(err)
		return -1
	}
	return aid
}

func GetUGroupByAdminID(aid int) *ugroupmodel.UGroup {
	var ugroup ugroupmodel.UGroup
	sqlStr := "SELECT * FROM ugroups where a_id = ?"
	if err := dao.GMysqlDB.Conn.Get(&ugroup, sqlStr, aid); err != nil {
		logger.Logger.Error(err)
		return nil
	}
	return &ugroup
}

func GetUGroupByPage(pageNo, pageSize int) []*ugroupmodel.UGroup {
	start := (pageNo - 1) * pageSize
	var ugroups []*ugroupmodel.UGroup
	sqlStr := "SELECT * FROM ugroups LIMIT ?,?"
	if err := dao.GMysqlDB.Conn.Select(&ugroups, sqlStr, start, pageSize); err != nil {
		logger.Logger.Error(err)
		return nil
	}
	return ugroups
}

func GetUGroupNameInfosWithoutAdmin() []*ugroupmodel.UGroupNameInfo {
	var UGroupNameInfo []*ugroupmodel.UGroupNameInfo
	sqlStr := "SELECT id,name FROM ugroups WHERE a_id= ?"
	if err := dao.GMysqlDB.Conn.Select(&UGroupNameInfo, sqlStr, -1); err != nil {
		logger.Logger.Error(err)
		return nil
	}
	return UGroupNameInfo
}

func GetUGroupWithoutAdminCount() int {
	var count int
	sqlStr := "SELECT COUNT(*) FROM ugroups WHERE a_id = ?"
	if err := dao.GMysqlDB.Conn.Get(&count, sqlStr, -1); err != nil {
		logger.Logger.Error(err)
		return -1
	}
	return count
}

func GetUGroupCount() int {
	var count int
	sqlStr := "SELECT COUNT(*) FROM ugroups"
	if err := dao.GMysqlDB.Conn.Get(&count, sqlStr); err != nil {
		logger.Logger.Error(err)
		return -1
	}
	return count
}

func GetAllUGroupNameInfos() []*ugroupmodel.UGroupNameInfo {
	var UGroupNameInfo []*ugroupmodel.UGroupNameInfo
	sqlStr := "SELECT id,name FROM ugroups"
	if err := dao.GMysqlDB.Conn.Select(&UGroupNameInfo, sqlStr); err != nil {
		logger.Logger.Error(err)
		return nil
	}
	return UGroupNameInfo
}

func UpdateUGroupWithAdminID(ugid int, aid int) error {
	sqlStr := "UPDATE ugroups SET a_id = ? where id = ?"
	if _, err := dao.GMysqlDB.Conn.Exec(sqlStr, aid, ugid); err != nil {
		logger.Logger.Error(err)
		return err
	}
	return nil
}
